package com.sg.service.base.component.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.component.req.*;
import com.sg.entity.OmsMessageLocationRemind;
import com.sg.service.base.component.MOmsMessageLocationRemindService;
import com.wicket.okrcomponent.common.annotations.BanAuto;
import com.wicket.okrcomponent.common.dto.FieldInfo;
import com.wicket.okrcomponent.common.exception.BizException;
import com.wicket.okrcomponent.dal.mapper.mbg.OmsMessageLocationRemindMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsMessageLocationRemindServiceImpl
        extends ServiceImpl<OmsMessageLocationRemindMapper, OmsMessageLocationRemind>
        implements MOmsMessageLocationRemindService {

    @Autowired
    OmsMessageLocationRemindMapper omsMessageLocationRemindMapper;

    /**
     * 2-5-04修改自动加消息数量[3996]
     * gen by moon at 9/14/2022, 4:09:16 PM
     */
    @Trace(operationName = "2-5-04修改自动加消息数量")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @BanAuto
    @Transactional(rollbackFor = Exception.class)
    public boolean updateAotoAdditionMessageNum(OmsMessageLocationRemind omsMessageLocationRemind) {
        omsMessageLocationRemind.setMessageNum(1L);
        // TODO: 2022/9/14 自动加
        UpdateWrapper<OmsMessageLocationRemind> uw = new UpdateWrapper<>();
        uw.eq("message_location_remind_id", omsMessageLocationRemind.getMessageLocationRemindId());
        if (omsMessageLocationRemind.getMessageNum() != null) {
            uw.setSql("message_num = message_num + " + omsMessageLocationRemind.getMessageNum());//ADD_NUMERICAL
            omsMessageLocationRemind.setMessageNum(null);
        }
        omsMessageLocationRemind.setOperateTime(CommonFunctionHelper.getCurrentTime());

        return super.update(omsMessageLocationRemind, uw);
    }

    /**
     * 2-5-04查询消息数量详情[3998]
     * gen by moon at 9/14/2022, 4:09:17 PM
     */
    @Trace(operationName = "2-5-04查询消息数量详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsMessageLocationRemind queryMessageNumDetail(QueryMessageNumDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsMessageLocationRemind> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsMessageLocationRemind> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsMessageLocationRemind retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-5-03执行自动减消息数量[4033]
     * gen by moon at 9/14/2022, 4:09:17 PM
     */
    @Trace(operationName = "2-5-03执行自动减消息数量")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @BanAuto
    @Transactional(rollbackFor = Exception.class)
    public boolean implementAotoSubtractionMessageNum(OmsMessageLocationRemind omsMessageLocationRemind) {
        omsMessageLocationRemind.setMessageNum(1L);//AUTO_SUB  TODO ruizhe dong
        // TODO: 2022/9/14 自定减
        UpdateWrapper<OmsMessageLocationRemind> uw = new UpdateWrapper<>();
        uw.eq("message_location_remind_id", omsMessageLocationRemind.getMessageLocationRemindId());
        if (omsMessageLocationRemind.getMessageNum() != null) {
            uw.setSql("message_num = message_num - " + omsMessageLocationRemind.getMessageNum());//ADD_NUMERICAL
            omsMessageLocationRemind.setMessageNum(null);
        }
        omsMessageLocationRemind.setOperateTime(CommonFunctionHelper.getCurrentTime());
        super.update(omsMessageLocationRemind, uw);
        return super.updateById(omsMessageLocationRemind);
    }

    /**
     * 2-5-04批量查询消息数量[4039]
     * gen by moon at 1/16/2024, 6:10:06 AM
     */
    @Trace(operationName = "2-5-04批量查询消息数量")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsMessageLocationRemind> batchQueryMessageNum(BatchQueryMessageNumReq reqDto) {

        try {
            QueryWrapper<OmsMessageLocationRemind> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getMessageNumList() == null || reqDto.getMessageNumList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("entity_id", reqDto.getMessageNumList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsMessageLocationRemind.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsMessageLocationRemind.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsMessageLocationRemind.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsMessageLocationRemind>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-5-04修改消息数量[4040]
     * gen by moon at 8/15/2023, 8:32:00 PM
     */
    @Trace(operationName = "2-5-04修改消息数量")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateMessageNum(OmsMessageLocationRemind omsMessageLocationRemind) {

        return super.updateById(omsMessageLocationRemind);
    }

    /**
     * 2-5-03查询消息数量列表[4588]
     * gen by moon at 10/1/2022, 11:27:58 PM
     */
    @Trace(operationName = "2-5-03查询消息数量列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsMessageLocationRemind> queryMessageNumList(QueryMessageNumListReq reqDto) {

        try {
            QueryWrapper<OmsMessageLocationRemind> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsMessageLocationRemind.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsMessageLocationRemind.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsMessageLocationRemind.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsMessageLocationRemind>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-5-03批量增个人消息数量账户[4590]
     * gen by moon at 10/1/2022, 11:28:00 PM
     */
    @Trace(operationName = "2-5-03批量增个人消息数量账户")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchAddUserMmessageQuantityAccount(List<OmsMessageLocationRemind> omsMessageLocationRemind) {

        if (omsMessageLocationRemind == null || omsMessageLocationRemind.size() == 0) {
            return false;
        }

        return super.saveBatch(omsMessageLocationRemind.stream().map(item -> {
            item.setMessageLocationRemindId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
            item.setIsValid("FALSE");
            return item;
        }).collect(Collectors.toList()));
    }

    /**
     * 2-5-04新增消息数量[6782]
     * gen by moon at 8/15/2023, 8:32:01 PM
     */
    @Trace(operationName = "2-5-04新增消息数量")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addMessageNum(OmsMessageLocationRemind omsMessageLocationRemind) {
        omsMessageLocationRemind.setMessageLocationRemindId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsMessageLocationRemind.setIsValid("FALSE");
        if (super.save(omsMessageLocationRemind)) {
            return omsMessageLocationRemind.getMessageLocationRemindId();
        } else {
            return "-1";
        }

    }

    /**
     * 2-5-04删除消息数量[7055]
     * gen by moon at 5/31/2023, 6:08:10 PM
     */
    @Trace(operationName = "2-5-04删除消息数量")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteMessageNum(String messageLocationRemindId) {

        return super.removeById(messageLocationRemindId);
    }

    /**
     * 2-5-04批量删消息数量[7054]
     * gen by moon at 5/31/2023, 7:59:11 PM
     */
    @Trace(operationName = "2-5-04批量删消息数量")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteMessageNum(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * 2-5-04查询消息数量By消息位置列表[7556]
     * gen by moon at 10/16/2023, 3:19:11 PM
     */
    @Trace(operationName = "2-5-04查询消息数量By消息位置列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsMessageLocationRemind> queryMessageNumByMsgLocationList(QueryMessageNumByMsgLocationListReq reqDto) {

        try {
            QueryWrapper<OmsMessageLocationRemind> queryWrapper = new QueryWrapper<>();

            if (reqDto.getMessagePositionList() == null || reqDto.getMessagePositionList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("message_location_type_code", reqDto.getMessagePositionList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsMessageLocationRemind.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsMessageLocationRemind.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsMessageLocationRemind.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsMessageLocationRemind>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-5-05批量查询消息数量列表ByCode[8081]
     * gen by moon at 12/5/2023, 12:03:36 AM
     */
    @Trace(operationName = "2-5-05批量查询消息数量列表ByCode")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsMessageLocationRemind> batchQueryMessageNumListByCode(BatchQueryMessageNumListByCodeReq reqDto) {

        try {
            QueryWrapper<OmsMessageLocationRemind> queryWrapper = new QueryWrapper<>();

            if (reqDto.getMessageNumLoopStartList() == null || reqDto.getMessageNumLoopStartList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("message_location_type_code", reqDto.getMessageNumLoopStartList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsMessageLocationRemind.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsMessageLocationRemind.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsMessageLocationRemind.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsMessageLocationRemind>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-5-04批量查询消息数量by归属内容ID[8140]
     * gen by moon at 1/16/2024, 6:10:09 AM
     */
    @Trace(operationName = "2-5-04批量查询消息数量by归属内容ID")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsMessageLocationRemind> batchQueryMessageNumByEntityId(BatchQueryMessageNumByEntityIdReq reqDto) {

        try {
            QueryWrapper<OmsMessageLocationRemind> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getMessageNumLoopStartList() == null || reqDto.getMessageNumLoopStartList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("extended_content_id", reqDto.getMessageNumLoopStartList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsMessageLocationRemind.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsMessageLocationRemind.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsMessageLocationRemind.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsMessageLocationRemind>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

}

